package gui;

import logic.Player;
import gamemngr.GameMngr;
import gamemngr.HelpMenu;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormToolkit;

import com.swtdesigner.SWTResourceManager;

public class NewDialog extends Dialog {

	private Combo expertCombo_5;
	private Combo expertCombo_4;
	private Combo expertCombo_3;
	private Combo expertCombo_2;
	private Combo expertCombo_1;
	private Label computerLabel;
	//private FormToolkit toolkit = new FormToolkit(Display.getCurrent());
	protected PlayerData[] result;
	protected Shell shell;
	private GameMngr game;
	Color[] playerColours;

	/**
	 * Create the dialog
	 * @param parent
	 * @param style
	 */
	public NewDialog(Shell parent, int style) {
		super(parent, style);
	}

	/**
	 * Create the dialog
	 * @param parent
	 */
	public NewDialog(Shell parent) {
		this(parent, SWT.NONE);
	}
	
	public NewDialog(Shell parent, GameMngr game, Color[] playerColours) {
		this(parent, SWT.NONE);
		this.game = game;
		this.playerColours = playerColours;
	}
	
	/**
	 * Open the dialog
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return result;
	}

	/**
	 * Create contents of the dialog
	 */
	protected void createContents() {
		result = new PlayerData[6];
		shell = new Shell(getParent(), SWT.APPLICATION_MODAL | SWT.TITLE | SWT.BORDER | SWT.CLOSE);
		shell.setLocation(RiskGUI.getNewPosition(getParent()));
		shell.setSize(276, 280);
		shell.setText("New Game");

		//final Form form = toolkit.createForm(shell);
		//final Composite body = form.getBody();
		//toolkit.paintBordersFor(body);
		//form.setText("New Form");
		//form.setBounds(89, 114, 0, 0);

		final Text player1Name = new Text(shell, SWT.NONE);
		player1Name.setBounds(55, 43, 102, 21);

		final Text player2Name = new Text(shell, SWT.NONE);
		player2Name.setBounds(55, 70, 102, 22);

		final Text player3Name = new Text(shell, SWT.NONE);
		player3Name.setBounds(55, 98, 102, 22);

		final Text player4Name = new Text(shell, SWT.NONE);
		player4Name.setBounds(55, 125, 102, 21);

		final Text player5Name = new Text(shell, SWT.NONE);
		player5Name.setBounds(55, 152, 102, 21);

		final Text player6Name = new Text(shell, SWT.NONE);
		player6Name.setBounds(55, 179, 102, 19);

		final Label playerLabel = new Label(shell, SWT.NONE);
		playerLabel.setText("Player 1");
		playerLabel.setBounds(10, 42, 39, 25);

		final Label playerLabel_1 = new Label(shell, SWT.NONE);
		playerLabel_1.setBounds(10, 69, 39, 25);
		playerLabel_1.setText("Player 2");

		final Label playerLabel_1_1 = new Label(shell, SWT.NONE);
		playerLabel_1_1.setBounds(10, 97, 39, 25);
		playerLabel_1_1.setText("Player 3");

		final Label playerLabel_1_2 = new Label(shell, SWT.NONE);
		playerLabel_1_2.setBounds(10, 124, 39, 25);
		playerLabel_1_2.setText("Player 4");

		final Label playerLabel_1_3 = new Label(shell, SWT.NONE);
		playerLabel_1_3.setBounds(10, 151, 39, 25);
		playerLabel_1_3.setText("Player 5");

		final Label playerLabel_1_4 = new Label(shell, SWT.SHADOW_NONE);
		playerLabel_1_4.setBounds(10, 178, 39, 25);
		playerLabel_1_4.setText("Player 6");

		final Button okButton_1 = new Button(shell, SWT.NONE);
		okButton_1.setText("Cancel");
		okButton_1.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				shell.dispose();
			}
		});
		okButton_1.setBounds(99, 216, 61, 23);

		final Label playerLabel_2 = new Label(shell, SWT.NONE);
		playerLabel_2.setFont(SWTResourceManager.getFont("", 11, SWT.BOLD));
		playerLabel_2.setBounds(0, 0, 173, 25);
		playerLabel_2.setText("Enter players names:");

		computerLabel = new Label(shell, SWT.NONE);
		computerLabel.setText("computer");
		computerLabel.setBounds(155, 27, 61, 15);

		expertCombo_1 = new Combo(shell, SWT.NONE);
		expertCombo_1.setText("level");
		expertCombo_1.setItems(new String[] {"Beginner", "Expert"});
		expertCombo_1.setBounds(200, 72, 62, 21);
		expertCombo_1.setEnabled(false);
		//toolkit.adapt(expertCombo_1, true, true);

		expertCombo_2 = new Combo(shell, SWT.NONE);
		expertCombo_2.setText("level");
		expertCombo_2.setItems(new String[] {"Beginner", "Expert"});
		expertCombo_2.setBounds(200, 98, 62, 21);
		expertCombo_2.setEnabled(false);
		//toolkit.adapt(expertCombo_2, true, true);

		expertCombo_3 = new Combo(shell, SWT.NONE);
		expertCombo_3.setText("level");
		expertCombo_3.setItems(new String[] {"Beginner", "Expert"});
		expertCombo_3.setBounds(200, 125, 62, 21);
		expertCombo_3.setEnabled(false);
		//toolkit.adapt(expertCombo_3, true, true);

		expertCombo_4 = new Combo(shell, SWT.NONE);
		expertCombo_4.setText("level");
		expertCombo_4.setItems(new String[] {"Beginner", "Expert"});
		expertCombo_4.setEnabled(false);
		expertCombo_4.setBounds(200, 152, 62, 21);
		//toolkit.adapt(expertCombo_4, true, true);

		expertCombo_5 = new Combo(shell, SWT.NONE);
		expertCombo_5.setText("level");
		expertCombo_5.setItems(new String[] {"Beginner", "Expert"});
		expertCombo_5.setBounds(200, 179, 62, 21);
		expertCombo_5.setEnabled(false);
		//toolkit.adapt(expertCombo_5, true, true);

		final Button computerButton_1 = new Button(shell, SWT.CHECK | SWT.FLAT);
		computerButton_1.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent arg0) {
				if(computerButton_1.getSelection()){
					expertCombo_1.setEnabled(true);
				} else {
					expertCombo_1.setEnabled(false);
				}
			}
		});
		//toolkit.adapt(computerButton_1, true, true);
		computerButton_1.setBounds(170, 72, 13, 16);

		final Button computerButton_2 = new Button(shell, SWT.CHECK | SWT.FLAT);
		computerButton_2.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent arg0) {
				if(computerButton_2.getSelection()){
					expertCombo_2.setEnabled(true);
				} else {
					expertCombo_2.setEnabled(false);
				}
			}
		});
		//toolkit.adapt(computerButton_2, true, true);
		computerButton_2.setBounds(170, 102, 13, 16);

		final Button computerButton_3 = new Button(shell, SWT.CHECK | SWT.FLAT);
		computerButton_3.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent arg0) {
				if(computerButton_3.getSelection()){
					expertCombo_3.setEnabled(true);
				} else {
					expertCombo_3.setEnabled(false);
				}
			}
		});
//		toolkit.adapt(computerButton_3, true, true);
		computerButton_3.setBounds(170, 132, 13, 16);

		final Button computerButton_4 = new Button(shell, SWT.CHECK | SWT.FLAT);
		computerButton_4.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent arg0) {
				if(computerButton_4.getSelection()){
					expertCombo_4.setEnabled(true);
				} else {
					expertCombo_4.setEnabled(false);
				}
			}
		});
//		toolkit.adapt(computerButton_4, true, true);
		computerButton_4.setBounds(170, 157, 13, 16);

		final Button computerButton_5 = new Button(shell, SWT.CHECK | SWT.FLAT);
		computerButton_5.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent arg0) {
				if(computerButton_5.getSelection()){
					expertCombo_5.setEnabled(true);
				} else {
					expertCombo_5.setEnabled(false);
				}
			}
		});
//		toolkit.adapt(computerButton_5, true, true);
		computerButton_5.setBounds(170, 182, 13, 16);
		
		final Button okButton = new Button(shell, SWT.NONE);
		okButton.setText("OK");
		okButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				result[0] = new PlayerData(player1Name.getText(), false, 1);
				result[1] = new PlayerData(player2Name.getText(), computerButton_1.getSelection(), expertCombo_1.getSelectionIndex()+1);
				result[2] = new PlayerData(player3Name.getText(), computerButton_2.getSelection(), expertCombo_2.getSelectionIndex()+1);
				result[3] = new PlayerData(player4Name.getText(), computerButton_3.getSelection(), expertCombo_3.getSelectionIndex()+1);
				result[4] = new PlayerData(player5Name.getText(), computerButton_4.getSelection(), expertCombo_4.getSelectionIndex()+1);
				result[5] = new PlayerData(player6Name.getText(), computerButton_5.getSelection(), expertCombo_5.getSelectionIndex()+1);
				int i = 0;
				for(PlayerData player : result){
					if(game.playerExists(player.getName())){
						AutoDialogMessegaes.displayErrorMsg("Players name error", "Can not enter the same name twice", shell);
						game.delPlayers();
						return;
					}
					if(!player.getName().equals("")){
						if(player.isComputer() && (player.getLevel()==0)){
							AutoDialogMessegaes.displayErrorMsg("Players level error", "You must choose the level for each of the computer players", shell);
							game.delPlayers();
							return;
						}
						Player newPlayer  = new Player(player.getName(), playerColours[i], game);
						if(player.isComputer()){
							newPlayer.markAsComputer();
							newPlayer.setLevel(player.getLevel());
						}
						game.addPlayer(newPlayer);
						i++;
					}
				}
				if(i==0 || i==1){
					AutoDialogMessegaes.displayErrorMsg("Number of players error", "There must be al least 2 players to start new game", shell);
					game.delPlayers();
					return;
				}
				shell.dispose();
			}
		});
		okButton.setBounds(55, 216, 39, 23);

		final Button helpButton = new Button(shell, SWT.FLAT);
		helpButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent arg0){
				game.initFromFile("newGameHelp.txt");
				HelpMenu help = game.getHelpMenu();
				MessageDialog msg = new MessageDialog(shell,"Help",null,help.toString(),MessageDialog.QUESTION,new String[] {"OK"},1);
				msg.open();
			}
		});
		helpButton.setFont(SWTResourceManager.getFont("", 10, SWT.BOLD));
//		toolkit.adapt(helpButton, true, true);
		helpButton.setText("?");
		helpButton.setBounds(166, 216, 26, 23);
		//
	}

}
